package Texas.Round;

import java.util.*;

import Texas.Player;
import Texas.PlayerIterator;
import Texas.Pot;
import Texas.Table;
import Texas.Console.ConsoleMenu;
import Texas.Console.ConsoleMenuItem;


public class Flop implements IRound{

	private Table _table;
	private ArrayList<Player> _players;
	private Player _winner;
	private int _raiseCounts = 0;
	
	private ConsoleMenuItem _cMI[] = {new ConsoleMenuItem("Check", 1), new ConsoleMenuItem("Raise", 2), 
			new ConsoleMenuItem("AllIn", 3), new ConsoleMenuItem("Fold", 4), new ConsoleMenuItem("Call", 5)};
	
	public Flop(Table table, ArrayList<Player> players) {
		_table = table;
		_players = players;
		_winner = null;
		
		prepareRound();
	}
	
	private void prepareRound() {
		_table.dealCards(3);
	}
	
	@Override
	public void start() {
		if((getCountOfActivePlayers() - getCountOfPlayersWhichAreAllIn()) > 1) {
			int minDediction = 0;
					
			for (PlayerIterator playerIterator = new PlayerIterator(_players, _table.getDealerButton()); playerIterator.hasNext();) {
				Player currentPlayer = playerIterator.next();
				if(currentPlayer.isAllIn()) {
					continue;
				}
				
				if(getCountOfActivePlayers() == 1 && getCountOfPlayersWhichAreAllIn() == 0) {
					_winner = currentPlayer;
				} else {
					int dediction = makeDecisionAndGetDediction(minDediction, currentPlayer);
				
					if(dediction > minDediction)
						minDediction = dediction;
				}
			}
		}
	}
	
	private int makeDecisionAndGetDediction(int minDediction, Player currentPlayer) {
		int begin = 0, end = _cMI.length;
		
		if(currentPlayer.getChips() <= minDediction) {
			begin = 2;
			end = _cMI.length-1; 
		} else {
			if(_raiseCounts < 3) {
				if(currentPlayer.getDediction() == minDediction) {
					begin = 0;
					end = _cMI.length-1; 
				} else		
					begin = 1;
			}
		}
	
		printInformation(currentPlayer, minDediction);
		try {
			switch(ConsoleMenu.selectOne(Arrays.copyOfRange(_cMI, begin, end))) {
					case 1:
						break;
					case 2:
						currentPlayer.raise(ConsoleMenu.getInt("Bitte geben Sie den Betrag ein: ", minDediction, currentPlayer.getChips()-1), minDediction);
						_raiseCounts++;
						break;
					case 3:
						currentPlayer.fold();
						break;
					case 4:
						currentPlayer.allIn(minDediction);
						break;
					case 5:
						currentPlayer.call(minDediction);
						break;		
			}
		} catch(Exception e) {
			System.err.println(e);
		}
		Pot.getInstance().addDediction(currentPlayer.getDediction());
		return currentPlayer.getDediction();
	}

	private int getCountOfActivePlayers() {
		int activePlayersCount = 0;
		
		for(Player player : _players) {
			if(!player.hasFolded()) {
				activePlayersCount++;
			}
		}
		
		return activePlayersCount;
	}
	
	private int getCountOfPlayersWhichAreAllIn() {
		int playerWhichAreAllInCount = 0;
		
		for(Player player : _players) {
			if(player.isAllIn()) {
				playerWhichAreAllInCount++;
			}			
		}
		
		return playerWhichAreAllInCount;
	}

	private void printInformation(Player currentPlayer, int minDediction) {
		System.out.printf("Name: %s\nIhre Karten: %s %s\n", currentPlayer.getName(), currentPlayer.getCard(0),
				currentPlayer.getCard(1));
		_table.printCommuntiyCards();
		System.out.printf("min. Einsatz: %d\nIhr Einsatz: %d\nChips: %d\n", minDediction,currentPlayer.getDediction(),
				currentPlayer.getChips());		
	}
	
	public Player getEarlyWinner() {
		return _winner;
	}
}
